home *** CD-ROM | disk | FTP | other *** search
/ EnigmA Amiga Run 1996 June / EnigmA AMIGA RUN 08 (1996)(G.R. Edizioni)(IT)[!][issue 1996-06][EARSAN CD VII].iso / earcd / gcc / ixemlsrc.lha / ixemul / library / ix_startup.c < prev    next >
C/C++ Source or Header  |  1996-03-13  |  5KB  |  171 lines

  1. /*
  2.  *  This file is part of ixemul.library for the Amiga.
  3.  *  Copyright (C) 1991, 1992  Markus M. Wild
  4.  *  Portions Copyright (C) 1994 Rafael W. Luebbert
  5.  *  Portions Copyright (C) 1995 Jeff Shepherd
  6.  *
  7.  *  This library is free software; you can redistribute it and/or
  8.  *  modify it under the terms of the GNU Library General Public
  9.  *  License as published by the Free Software Foundation; either
  10.  *  version 2 of the License, or (at your option) any later version.
  11.  *
  12.  *  This library is distributed in the hope that it will be useful,
  13.  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
  14.  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  15.  *  Library General Public License for more details.
  16.  *
  17.  *  You should have received a copy of the GNU Library General Public
  18.  *  License along with this library; if not, write to the Free
  19.  *  Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  20.  *
  21.  *  $Id: ix_startup.c,v 1.5 1994/06/19 15:13:22 rluebbert Exp $
  22.  *
  23.  *  $Log: ix_startup.c,v $
  24.  *  Revision 1.5  1994/06/19  15:13:22  rluebbert
  25.  *  *** empty log message ***
  26.  *
  27.  *  Revision 1.3  1992/08/09  20:55:51  amiga
  28.  *  import sysbase
  29.  *
  30.  *  Revision 1.2  1992/07/04  19:18:21  mwild
  31.  *  remove SIGWINCH handler before returning
  32.  *
  33.  * Revision 1.1  1992/05/14  19:55:40  mwild
  34.  * Initial revision
  35.  *
  36.  */
  37.  
  38. #define KERNEL
  39. #include "ixemul.h"
  40. #include <sys/wait.h>
  41. #include "kprintf.h"
  42.  
  43. /* this just jumps right back into ix_startup() */
  44.  
  45.  
  46. /*
  47.  * Note: I kept the partition into startup and _main(), although in this
  48.  *       case, both functions could be done in one function, since this is
  49.  *       a library, and the user can't override _main anyway but globally...
  50.  */
  51.  
  52. int
  53. ix_startup (char *aline, int alen,
  54.         int expand, char *wb_default_window, u_int main, int *real_errno)
  55. {
  56.   struct Process *me = (struct Process *) SysBase->ThisTask;
  57.   int exit_val;
  58.   struct WBStartup *wb_msg = NULL;
  59.   int fd;
  60.  
  61.   /*
  62.    * The following code to reset the fpu might not be necessary, BUT since
  63.    * a CLI shell doesn't spawn a new process when executing a command - it 
  64.    * insteads calls the command like a subroutine - it depends on the Shell
  65.    * whether the fpu is setup correctly. And I don't like to depend on any
  66.    * thing ;-)
  67.    */
  68.  
  69.   if (gotanfpu())
  70.     resetfpu();
  71.   /* first deal with WB messages, since those HAVE to be answered properly,
  72.    * even if we should fail later (memory, whatever..) */
  73.  
  74.   if (! me->pr_CLI)
  75.     {
  76.       /* we have been started by Workbench. Get the StartupMsg */
  77.       WaitPort (& me->pr_MsgPort);
  78.       wb_msg = (struct WBStartup *) GetMsg (& me->pr_MsgPort);
  79.       /* further processing in _main () */
  80.     }
  81.   else
  82.     {
  83.       struct CommandLineInterface *cli = (void *)BADDR(me->pr_CLI);
  84.  
  85.       /* for usage by sys_exit() for example */
  86.       KPRINTF (("CLI command line '%s'\n", aline));
  87.       u.u_argline = aline;
  88.       u.u_arglinelen = alen;
  89.       u.u_segs = cli->cli_Module;
  90.     }
  91.  
  92.   u.u_expand_cmd_line = expand;
  93.  
  94.   /* put some AmiTCP and AS225 in here since it can't go into ix_open
  95.    * I need a pointer to the REAL errno
  96.    */
  97.   if (real_errno)
  98.    {
  99.       extern void NET_set_errno(int *);
  100.  
  101.       u.u_errno = real_errno;
  102. #ifdef USE_NET
  103.       if (u.u_ixnetbase)
  104.     NET_set_errno(real_errno);
  105. #endif
  106.    }
  107.  
  108.   KPRINTF (("&errno = %lx\n", real_errno));
  109.  
  110.   exit_val = _setjmp (u.u_jmp_buf);
  111.   if (! exit_val)
  112.     {
  113.       /* from now on it's safe to allow signals */
  114.       syscall (SYS_sigsetmask, 0);
  115.       /* the first time thru call the program */
  116.       KPRINTF (("calling __main()\n"));
  117.       if (me->pr_CLI)
  118.         syscall (SYS__main, aline, alen, main);
  119.       else
  120.     syscall (SYS__main, wb_msg, wb_default_window, main);
  121.       /* NORETURN */
  122.     }
  123.   /* in this case we came from a longjmp-call */
  124.  
  125.   exit_val = u.p_xstat;
  126.  
  127.   ix_remove_sigwinch ();
  128.  
  129.   /* had to move the closing of files out of ix_close(), as close() may 
  130.      actually wait for the last packet to arrive, and inside ix_close() we're
  131.      inside Forbid, and may thus never wait! */
  132.  
  133.   /* close all files */
  134.   for (fd = 0; fd < NOFILE; fd++) 
  135.     if (u.u_ofile[fd]) syscall (SYS_close, fd);
  136.  
  137.   /* if at all possible, free memory before entering Forbid ! (Semaphore
  138.      problems..) */
  139.   all_free ();
  140.  
  141.   /*
  142.    * If we were traced by a debugger who got us through a ptrace 'attach',
  143.    * we need to unlink us from our parent and to give it a signal telling
  144.    * it that we've died.
  145.    */
  146.   if (u.p_pptr && u.p_pptr != (struct Process *) 1)
  147.     {
  148.       send_death_msg(&u);
  149.       Permit();  /* send_death_msg did the matching Forbid() */
  150.     }
  151.  
  152.   /* if started from workbench, Forbid(), since on reply WB will deallocate
  153.    * our task... */
  154.   if (! me->pr_CLI)
  155.     {
  156.       Forbid ();
  157.       ReplyMsg ((struct Message *) wb_msg);
  158.     }
  159.  
  160.   return WEXITSTATUS(exit_val);
  161. }
  162.  
  163. void
  164. _exit (int retcode)
  165. {
  166.   /* ignore all signals from now on */
  167.   syscall (SYS_sigsetmask, ~0);
  168.   u.p_xstat = W_EXITCODE(retcode, 0);
  169.   _longjmp (u.u_jmp_buf, 1);
  170. }
  171.